package net.muchine.web.front.controller.admin;

import java.util.List;
import java.util.Locale;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import net.muchine.core.domain.user.Preference;
import net.muchine.core.domain.user.User;
import net.muchine.core.domain.user.UserSearch;
import net.muchine.core.service.user.PreferenceService;
import net.muchine.core.service.user.UserService;
import net.muchine.core.support.util.Converter;
import net.muchine.web.common.Pages;
import net.muchine.web.front.controller.AdminController;
import net.muchine.web.front.forms.Account;
import net.muchine.web.front.forms.Password;

@Controller("adminUserController")
public class UserController extends AdminController {
	@Autowired private UserService userService;
	@Autowired private PreferenceService prefService;
	
	@Autowired protected Validator validator;
	@Autowired private Converter converter;
			
	private static final String MODEL_USERS = "users";
	private static final String MODEL_ACCOUNT = "account";
	
	@RequestMapping("/users")
	public String listUsers(ModelMap model) {
		List<User> users = userService.findByParameters(new UserSearch());
		String timezone = session.getPreference().getTimeZone();
		
		// set up timezone of current user
		for (User user : users) {
			user.setLocalCreatedTime(
				converter.convertDateWithTimeZone(user.getCreatedTime(), timezone));
			user.setLocalLastLoginTime(
				converter.convertDateWithTimeZone(user.getLastLoginTime(), timezone));
		}
		
		model.put(MODEL_USERS, users);
		return Pages.ADMIN_USERS_LIST;
	}
	
	@RequestMapping(value="/users", method=RequestMethod.POST)
	public String createUser(
			  @ModelAttribute @Valid Account account
			, BindingResult result
			, ModelMap model
			, Locale locale) {
		
		if (result.hasErrors()) {
			this.logErrors(result);
			
			this.setAccountCodeLists(model);
			return Pages.ADMIN_USERS_NEW; 
		}
		
		User user = account.getUser();
		Password password = account.getPassword();
		Preference preference = account.getPreference();
		
		user.setPassword(password.getNewPassword());
		userService.create(user);
		prefService.create(preference);
		
		log.info("A user has been created... id: " + user.getId());
		
		this.setSuccessCreationNotice(model, locale);
		return this.newUser(model);
	}
	
	@RequestMapping("/users/new")
	public String newUser(ModelMap model) {	
		model.put(MODEL_ACCOUNT, new Account().initialize());
		
		this.setAccountCodeLists(model);
		return Pages.ADMIN_USERS_NEW;
	}	
}
